package cn.iocoder.yudao.module.crm.service.mobileencryptconfig;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.crm.controller.admin.mobileencryptconfig.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.mobileencryptconfig.MobileEncryptConfigDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crm.dal.mysql.mobileencryptconfig.MobileEncryptConfigMapper;

import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 号码隐私设置 Service 实现类
 *
 * @author 数创万维
 */
@Service
@Validated
public class MobileEncryptConfigServiceImpl implements MobileEncryptConfigService {

    @Resource
    private MobileEncryptConfigMapper mobileEncryptConfigMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createMobileEncryptConfig(MobileEncryptConfigSaveReqVO createReqVO) {
        // 插入
        MobileEncryptConfigDO mobileEncryptConfig = BeanUtils.toBean(createReqVO, MobileEncryptConfigDO.class);
        mobileEncryptConfigMapper.insert(mobileEncryptConfig);
        // 返回
        return mobileEncryptConfig.getId();
    }

    @Override
    public void updateMobileEncryptConfig(MobileEncryptConfigSaveReqVO updateReqVO) {
        // 更新
        MobileEncryptConfigDO encryptConfig = getMobileEncryptConfig();
        if (Objects.isNull(encryptConfig)) {
            createMobileEncryptConfig(updateReqVO);
        } else {
            MobileEncryptConfigDO updateObj = BeanUtils.toBean(updateReqVO, MobileEncryptConfigDO.class);
            mobileEncryptConfigMapper.updateById(updateObj);
        }
    }


    @Override
    public MobileEncryptConfigDO getMobileEncryptConfig() {
        return mobileEncryptConfigMapper.selectOne(MobileEncryptConfigDO::getDeleted, 0);
    }


    @Override
    public MobileEncryptConfigDO useEncrypt() {
        MobileEncryptConfigDO mobileEncryptConfig = getMobileEncryptConfig();
        if (Objects.isNull(mobileEncryptConfig) || Objects.equals(0, mobileEncryptConfig.getStatus())) {
            return null;
        }
        AdminUserRespDTO user = adminUserApi.getUser(getLoginUserId());
        if (mobileEncryptConfig.getDeptIds().contains(user.getDeptId())) {
            return mobileEncryptConfig;
        }
        if (mobileEncryptConfig.getUserIds().contains(user.getId())) {
            return mobileEncryptConfig;
        }
        return null;
    }

    @Override
    public String convertEncryptMobile(MobileEncryptConfigDO config, String mobile) {
        if (Objects.isNull(config) || StrUtil.isEmpty(mobile)) {
            return mobile;
        }
        if (mobile.length() < config.getPrefix() + config.getSuffix()) {
            return mobile;
        }
        StringBuilder text = new StringBuilder();
        int len = mobile.length() - config.getPrefix() - config.getSuffix();
        for (int i = 0; i < len; i++) {
            text.append(config.getText());
        }
        return StrUtil.replace(mobile, config.getPrefix(), config.getPrefix() + len, text.toString());
    }
}